import { Article } from './home'

type SeartchType = {
  suggestions: string[]
  historys: string[]
  results: Article[]
}
export type SearAction =
  | {
      type: 'search/saveSuggestions'
      payload: string[]
    }
  | {
      type: 'search/closeSuggestios'
    }
  | {
      type: 'search/addSuggeList'
      payload: string[]
    }
  | {
      type: 'search/clearHistorys'
    }
  | {
      type: 'search/SearchResults'
      payload: Article[]
    }
const initValue: SeartchType = {
  suggestions: [],
  historys: [],
  results: [],
}
export default function reducer(state = initValue, action: SearAction) {
  if (action.type === 'search/saveSuggestions') {
    return {
      ...state,
      suggestions: action.payload,
    }
  }
  if (action.type === 'search/closeSuggestios') {
    return {
      ...state,
      suggestions: [],
    }
  }
  if (action.type === 'search/addSuggeList') {
    return {
      ...state,
      historys: action.payload,
    }
  }
  if (action.type === 'search/clearHistorys') {
    return {
      ...state,
      historys: [],
    }
  }
  if (action.type === 'search/SearchResults') {
    return {
      ...state,
      results: [...state.results, ...action.payload],
    }
  }
  return state
}
